﻿using DataSyncCom;
using DataSyncCommon;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SupDataSync.ChildForms
{
    public partial class FrmDataDelte : Form
    {
        public FrmDataDelte()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                if (textBox1.Text.Trim() == "Data Source='{0}';Port='{1}';User Id='{2}';Password='{3}';pooling=true;character set=utf8;")
                {
                    MessageBox.Show("请先设置连接地址", "提示");
                    textBox1.Focus();
                    return;
                }
                string sql = "SELECT SCHEMA_NAME AS DBNAME FROM INFORMATION_SCHEMA.SCHEMATA order by SCHEMA_NAME";
                DataSet ds = null;
                DbHelperMySql.ExecuteQueryByConnstr(textBox1.Text, sql, out ds);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    SysConfig.ConnectionStr = textBox1.Text.Trim();
                    MessageBox.Show("访问成功", "提示");
                }
            }
            catch (Exception)
            {
                MessageBox.Show("未成功访问到数据库，请检查链接地址是否正确", "提示");
            }

        }

        private void textBox2_Leave(object sender, EventArgs e)
        {
            try
            {
                if (textBox2.Text.Trim() == "")
                {
                    return;
                }
                if (textBox1.Text.Trim() == "Data Source='{0}';Port='{1}';User Id='{2}';Password='{3}';pooling=true;character set=utf8;" && textBox2.Text != "")
                {
                    MessageBox.Show("请先设置连接地址", "提示");
                    return;
                }
                string sql = "select F_Name from pms.t_pms_tenant where TenantId = " + textBox2.Text.Trim() + ";";
                DataSet ds = null;
                SysConfig.ConnectionStr = textBox1.Text.Trim();
                DbHelperMySql.ExecuteQuery(sql, out ds);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    label2.Text = "企业：";
                    label2.Text = label2.Text + ds.Tables["Table"].Rows[0]["F_Name"].ToString();
                }
                else
                {
                    MessageBox.Show("未能查找到企业", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    textBox2.Focus();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("服务器异常！"+ex.Message, "提示");
            }
        }

        private void textBox3_Leave(object sender, EventArgs e)
        {
            try
            {
                if (textBox3.Text.Trim() == "")
                {
                    return;
                }
                if (textBox1.Text.Trim() == "Data Source='{0}';Port='{1}';User Id='{2}';Password='{3}';pooling=true;character set=utf8;" && textBox3.Text != "")
                {
                    MessageBox.Show("请先设置连接地址", "提示");
                    return;
                }
                string sql = "select custormername from pub.t_pub_customer where ID in (select pubcustomerid from pms.t_pms_customers where tenantid = " + textBox2.Text.Trim() + ") and customercode = " + textBox3.Text.Trim() + ";";
                DataSet ds = null;
                SysConfig.ConnectionStr = textBox1.Text.Trim();
                DbHelperMySql.ExecuteQuery(sql, out ds);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    label3.Text = "客户：";
                    label3.Text = label3.Text + ds.Tables["Table"].Rows[0]["custormername"].ToString();
                }
                else
                {
                    MessageBox.Show("未能查找到客户", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    textBox3.Focus();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("服务器异常！"+ex.Message, "提示");
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (label2.Text.Equals("企业：未选择") || label3.Text.Equals("客户：未选择"))
            {
                MessageBox.Show("请选择企业和客户", "提示");
                return;
            }
            var tid = Convert.ToInt32(textBox2.Text.Trim());
            var ccode = Convert.ToInt32(textBox3.Text.Trim());
            using (var db = DbHelperMySql.GetSqlSugarDb())
            {
                db.Ado.BeginTran();
                var a = db.Ado.ExecuteCommand($"delete from pbms_sup.ba_bar_flag  where TenantId={tid} and customercode='{ccode}';");
                a += db.Ado.ExecuteCommand($"delete from pbms_sup.ba_bar_ruledetail  where TenantId={tid} and customercode='{ccode}';");
                a += db.Ado.ExecuteCommand($"delete from pbms_sup.ba_bar_version  where TenantId={tid} and customercode='{ccode}';");
                a += db.Ado.ExecuteCommand($"delete from pbms_sup.ba_customer  where TenantId={tid} and customercode='{ccode}';");
                a += db.Ado.ExecuteCommand($"delete from pbms_sup.ba_mat_unit_chg  where TenantId={tid} and customercode='{ccode}';");
                a += db.Ado.ExecuteCommand($"delete from pbms_sup.ba_material  where TenantId={tid} and customercode='{ccode}';");
                a += db.Ado.ExecuteCommand($"delete from pbms_sup.ba_material_type  where TenantId={tid} and customercode='{ccode}';");
                a += db.Ado.ExecuteCommand($"delete from pbms_sup.ba_name_define  where TenantId={tid};");
                a += db.Ado.ExecuteCommand($"delete from pbms_sup.ba_supplier  where TenantId={tid} and customercode='{ccode}';");
                a += db.Ado.ExecuteCommand($"delete from pbms_sup.ba_unit  where TenantId={tid} and customercode='{ccode}';");
                a += db.Ado.ExecuteCommand($"delete from pub.supplier where pub.supplier.TENANTID={tid} and CD='{ccode}';");
                a += db.Ado.ExecuteCommand($"delete from pub.measure_all where TENANTID={tid} and CUSTOMERCODE='{ccode}';");
                a += db.Ado.ExecuteCommand($"delete from pub.measure where TENANTID={tid};");
                a += db.Ado.ExecuteCommand($"delete from pub.materialtype_all where TENANTID={tid} and CUSTOMERCODE='{ccode}';");
                a += db.Ado.ExecuteCommand($"delete from pub.materialtype where TENANTID={tid};");
                a += db.Ado.ExecuteCommand($"delete from pub.material_all where TENANTID={tid} and CUSTOMERCODE='{ccode}';");
                a += db.Ado.ExecuteCommand($"delete from pub.material where TENANTID={tid};");
                a += db.Ado.ExecuteCommand($"delete from pub.measure_all where TENANTID={tid} and CUSTOMERCODE='{ccode}';");
                a += db.Ado.ExecuteCommand($"delete from pub.measure where TENANTID={tid};");
                a += db.Ado.ExecuteCommand($"delete from pub.material_measure_convert_all where TENANTID={tid} and CUSTOMERCODE='{ccode}';");
                a += db.Ado.ExecuteCommand($"delete from pub.material_measure_convert where TENANTID={tid};");
                if (a.ToString().Equals("0"))
                {
                    MessageBox.Show("清理成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("清理失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void 默认连接地址ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.textBox1.Text = "Data Source='49.4.70.226';Port='3306';User Id='root';Password='aaabbb123';pooling=true;character set=utf8;";
            MessageBox.Show("设置成功", "提示");
        }

        private void FromDataDelete_click(object sender, EventArgs e)
        {
            button2.Focus();
        }

        private void 帮助ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("1.先输入正确的链接地址；" +
                "\n2.输入企业编号查询企业；" +
                "\n3.输入客户代码查询客户；" +
                "\n4.点击[清理数据]进行数据清理，提示“清理成功”表示操作成功；" +
                "\n5.点击[访问数据库]可测试连接是否成功。","帮助");
        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
